package com.lee.crawler.gov.strategy.list;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.lee.crawler.gov.strategy.FzcangshanArticleStrategy;

@Component
public class FzcangshanListStrategy implements ListStrategy {

	private static final Pattern pageCountPattern = Pattern.compile("pageCount : (\\d+)");
	private static final String urlSuffix = "index_%d.htm";

	@Autowired
	private FzcangshanArticleStrategy fzcangshanArticleStrategy;

	public static void main(String[] args) throws UnsupportedEncodingException {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml",
				"spring/applicationContext-datasource.xml", "spring/applicationContext-redis.xml");
		context.start();
		Arrays.asList(context.getBeanDefinitionNames()).forEach(System.out::println);

		FzcangshanListStrategy bean = (FzcangshanListStrategy) context.getBean("fzcangshanListStrategy");
		bean.saveCrawlerArticles();

		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (context != null) {
				context.close();
			}
		}

	}

	@Override
	public String setCategroyUrl() {
		return null;
	}

	@Override
	public void saveCrawlerArticles() {
		Stream.of("http://www.fzcangshan.gov.cn/xxgk/tzgg/gsgg/", "http://www.fzcangshan.gov.cn/xxgk/tzgg/bmgg/")
				.forEach(url -> {
					Document content = getContent(url);
					String text = content.html();
					Matcher matcher = pageCountPattern.matcher(text);
					if (matcher.find()) {
						Integer pageCount = Integer.valueOf(matcher.group(1));
						for (int i = 0; i < pageCount; i++) {
							String realUrl = i == 0 ? url : url + String.format(urlSuffix, i);
							savePage(realUrl);
						}
					}
				});
	}

	private void savePage(String pageUrl) {
		Document contentStr = getContent(pageUrl);
		List<String> links = contentStr.select("ul.list-ndbg li a").eachAttr("abs:href");
		links.stream().filter(link -> link.matches(".*\\.htm(l)?$")).forEach(link -> {
			fzcangshanArticleStrategy.saveCrawlerArticle(link);
		});
	}

}
